package com.GlobalPaint.app.ui.Home.talent;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.TalentAdapter;
import com.GlobalPaint.app.Base.DialogWaiting;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.TalentBean;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

public class TalentActivity extends AppCompatActivity implements View.OnClickListener{
    private XRecyclerView talent_xrecyler;
    private LinearLayoutManager linearLayoutManager;
    private TalentAdapter adapter;
    private List<TalentBean.DataBean> list;
    private TalentBean bean;
    private ImageView iv_goback;
    private TextView tv_common_center;
    private int page = 1;
    private Dialog dlgWaiting2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talent);
        initView();
    }
    private void initView() {
        iv_goback = (ImageView) findViewById(R.id.iv_goback);
        iv_goback.setOnClickListener(this);
        tv_common_center = (TextView) findViewById(R.id.tv_common_center);
        tv_common_center.setText("人才库");
        talent_xrecyler = (XRecyclerView) findViewById(R.id.talent_xrecyler);
        linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        talent_xrecyler.setLayoutManager(linearLayoutManager);
        getList();
        initLoad();
    }



    private void getList() {
        dlgWaiting2 = DialogWaiting.createDialogWaiting(TalentActivity.this, "加载中......");
        dlgWaiting2.show();
        String url= Constants.JASON_SERVICE_URL+"/renren-security/appapi/resume/querylist?fResumeId=5&page=1";
        DataManager.getInst().getHttpRequestJsonClass(url, "dd", TalentBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                bean=(TalentBean)o;
                if(bean.getStatus()==1){
                    if( bean.getData()!=null){
                        list=bean.getData();
                        adapter=new TalentAdapter(list,TalentActivity.this);
                        talent_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(TalentActivity.this,bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                talent_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();
                if(list!=null&&list.size()>0) {
                    list.clear();
                    Toast.makeText(TalentActivity.this, "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    talent_xrecyler.refreshComplete();

                }
            }
        });
    }
    private void initLoad() {
        talent_xrecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url= Constants.JASON_SERVICE_URL+"/renren-security/appapi/resume/querylist?fResumeId=5&page="+1;
                DataManager.getInst().getHttpRequestJsonClass(url, "dd", TalentBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        page = 1;
                        TalentBean talentBean=(TalentBean)o;
                        if( bean.getStatus() == 1){
                            if (bean.getData() != null && bean.getData().size()>0){
                                list.clear();
                                list.addAll(talentBean.getData());
                                adapter.notifyDataSetChanged();
                                talent_xrecyler.loadMoreComplete();
                            }else {
                                Toast.makeText(TalentActivity.this,"刷新失败",Toast.LENGTH_LONG).show();
                            }
                        }
                        else {
                            Toast.makeText(TalentActivity.this,"刷新失败",Toast.LENGTH_LONG).show();
                        }
                        talent_xrecyler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        talent_xrecyler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
                page++;
                String url= Constants.JASON_SERVICE_URL+"/renren-security/appapi/resume/querylist?fResumeId=5&page="+page;
                DataManager.getInst().getHttpRequestJsonClass(url, "dd", TalentBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        TalentBean talentBean=(TalentBean)o;
                        if(talentBean.getStatus()==1){
                            if (talentBean.getData()!=null&&talentBean.getData().size()>0){
                                list.addAll(talentBean.getData());
                            }else {
                                page = page - 1;
                                Toast.makeText(TalentActivity.this,"暂无更多数据",Toast.LENGTH_LONG).show();
                            }
                        }
                       else {
                            page = page - 1;
                            Toast.makeText(TalentActivity.this,"暂无更多数据",Toast.LENGTH_LONG).show();
                        }
                        talent_xrecyler.loadMoreComplete();
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        page = page - 1;
                            Toast.makeText(TalentActivity.this, "暂无更多数据", Toast.LENGTH_LONG).show();
                            talent_xrecyler.loadMoreComplete();
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
        }
    }
}
